/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package facelets.component;

import java.util.ResourceBundle;
import java.util.TimeZone;

import javax.faces.convert.DateTimeConverter;

import org.apache.myfaces.custom.calendar.HtmlInputCalendar;

import com.sun.facelets.tag.jsf.ComponentConfig;
import com.sun.facelets.tag.jsf.ComponentHandler;

/**
 * @author nachoe
 *
 */
public class InputCalendarComponentHandler extends ComponentHandler { // NO_UCD
	
	public InputCalendarComponentHandler(ComponentConfig cfg) {
		super(cfg);
	}
	
	private static DateTimeConverter dtc = new DateTimeConverter();
	static {
		dtc.setPattern("dd/MM/yyyy");
		dtc.setTimeZone(TimeZone.getDefault());
	}
	/* (non-Javadoc)
	 * @see com.sun.facelets.tag.MetaTagHandler#setAttributes(com.sun.facelets.FaceletContext, java.lang.Object)
	 */
	public void setAttributes(com.sun.facelets.FaceletContext ctx, Object instance) {
		super.setAttributes(ctx, instance);
		HtmlInputCalendar cal = (HtmlInputCalendar)instance;
		
		if ( getAttribute("renderAsPopup") == null )
			cal.setRenderAsPopup(true);
		if ( getAttribute("renderPopupButtonAsImage") == null )
			cal.setRenderPopupButtonAsImage(true);
		if ( getAttribute("popupDateFormat") == null ) 
			cal.setPopupDateFormat("dd/MM/yyyy");
		
		if ( getAttribute("helpText") == null )
			cal.setHelpText("dd/mm/yyyy");
		
		if ( getAttribute("size") == null )
			cal.setSize(10);
		
		cal.setConverter(dtc);
		
		//if ( getAttribute("popupButtonImageUrl") == null )
			//cal.setPopupButtonImageUrl("./images/calendar.gif");
		//if ( getAttribute("styleClass") == null )
			//cal.setStyleClass("inputCalendar");
		
		cal.setOnchange("check_date(this)" +((getAttribute("onchange")!=null)? ";"+getAttribute("onchange").getValue():""));
		
		ResourceBundle rb = ResourceBundle.getBundle("org.apache.myfaces.custom.ysf.calendar.resource.properties.mensajes_calendario", ctx.getFacesContext().getViewRoot().getLocale());
		if ( getAttribute("popupTodayString") == null )
			cal.setPopupTodayString(rb.getString("jscalendarTodayString"));
		if ( getAttribute("popupWeekString") == null )
			cal.setPopupWeekString(rb.getString("jscalendarWeekString"));
		
		// If not disable redefine the size of the component.
		if ( cal.isDisabled() )
			if ( getAttribute("size") == null )
				cal.setSize(16);
	}
}